package config

import (
	"gopkg.in/ini.v1"
	"log"
)

var (
	Cfg          *ini.File
	ServerPort   int
	AppMode      string
	Db           string
	DbHost       string
	DBPort       int
	DbUser       string
	DbPassWord   string
	DbName       string
	DbTimeOut    string
	MaxOpenConns int
	MaxIdleConns int
	JwtSign      string
	JwtExp       int
)

func init() {
	var err error
	Cfg, err = ini.Load("./config/config.ini")
	if err != nil {
		log.Fatalf("Fail to parse 'conf/config.ini': %v", err)
	}
	loadService()
	loadMysql()
	loadJwt()
}

func loadJwt() {
	JwtSign = Cfg.Section("jwt").Key("Sign").String()
	JwtExp, _ = Cfg.Section("jwt").Key("Exp").Int()
}

func loadService() {
	ServerPort, _ = Cfg.Section("service").Key("ServerPort").Int()
	AppMode = Cfg.Section("service").Key("AppMode").String()
}

func loadMysql() {
	Db = Cfg.Section("mysql").Key("Db").String()
	DbHost = Cfg.Section("mysql").Key("DbHost").String()
	DBPort, _ = Cfg.Section("mysql").Key("DBPort").Int()
	DbUser = Cfg.Section("mysql").Key("DbUser").String()
	DbPassWord = Cfg.Section("mysql").Key("DbPassWord").String()
	DbName = Cfg.Section("mysql").Key("DbName").String()
	DbTimeOut = Cfg.Section("mysql").Key("DbTimeOut").String()
	MaxOpenConns, _ = Cfg.Section("mysql").Key("MaxOpenConns").Int()
	MaxIdleConns, _ = Cfg.Section("mysql").Key("MaxIdleConns").Int()
}
